import React, {useState,useEffect} from "react";
import {get, trim} from "lodash";
import {Modal, Form, Input, Checkbox, Button, Col} from "antd/lib";
import { wrap as wrapDialog, DialogPropType } from "@/components/DialogWrapper";
import DynamicComponent from "@/components/DynamicComponent";
import {policy} from "@/services/policy";
import {UserProfile} from "@/components/proptypes";
import notification from "@/services/notification";
import {currentUser} from "@/services/auth";
import Customer from "@/services/customer";
import {StarOutlined, UploadOutlined} from "@ant-design/icons";
import Uploads from "@/services/uploads";
import Attaches from "@/services/attaches";
import Upload from "antd/lib/upload";
import ContractApply from "@/services/contractapply";
import moment from "moment";

function CreateCustomertempDialog({ customertemp, dialog }) {
  const [dataLoaded, setDataLoaded] = useState(customertemp.id ===0 ? true : false);
  const [id, setid] = useState(customertemp.id);
  const [code, setCode] = useState("");
  const [name, setName] = useState("");
  const [representative, setrepresentative] = useState("");
  const [mobile, setmobile] = useState("");
  const [address, setaddress] = useState("");
  const [is_active, setis_active] = useState(true);
  const [isValid, setIsValid] = useState(false);
  const [saveInProgress, setSaveInProgress] = useState(false);
  const isCreateCustomertempEnabled = useState(true);//policy.isAdmin() || policy.isDefault();

  useEffect(() => {
    // if(id===0){
    //   Customer.getid({id: 0}).then(data => {
    //        setid(data[0].id);
    //     })
    //     .catch(error => {
    //     });
    // }
    if (customertemp.id === 0){
        let item = {
          tblname: "customertemp",
          bill_date: moment(),
          type: 0,
          tblfield: "strcustomercode",
        }
        ContractApply.getcode(item).then(data => {
          setCode(data[0].code);
          setDataLoaded(true);
        })
          .catch(error => {
            setDataLoaded(true);
          });
    }else{
        setCode(customertemp.strcustomercode);
        setName(customertemp.strcustomername);
        setrepresentative(customertemp.representative);
        setmobile(customertemp.mobile);
        setaddress(customertemp.address);
        setis_active(customertemp.blnisinactive);
        setDataLoaded(true);
      }
  }, [customertemp]);

  function handleCodeChange(event) {
    const value = trim(event.target.value);
    setCode(value);
    setIsValid(value !== "" && name !== "");
  }

  function handleis_activeChange(event) {
    const value = event.target.checked;
    setis_active(value);
    setIsValid(code !== "" && name !== "");
  }
  function handleNameChange(event) {
    const value = trim(event.target.value);
    setName(value);
    setIsValid(value !== "" && code !== "" );
  }
  function handlerepresentativeChange(event) {
    const value = trim(event.target.value);
    setrepresentative(value);
    setIsValid(code !== "" && name !== "" );
  }
  function handlemobileChange(event) {
    const value = trim(event.target.value);
    setmobile(value);
    setIsValid(code !== "" && name !== "" );
  }
  function handleaddressChange(event) {
    const value = trim(event.target.value);
    setaddress(value);
    setIsValid(code !== "" && name !== "" );
  }
  function handleSubmit() {
    setSaveInProgress(true);
   let data={id: id,//
            userid: currentUser.id,//--操作员ID
            strcustomercode: code,
            strcustomername: name,
            representative: representative,
            mobile: mobile,
            address: address,
            blnisinactive: is_active,}
    if(customertemp.id===0) {
      Customer.createcustomer(data)
        .then(() => {
          dialog.close({ success: true});
        })
        .catch(error => {
          notification.error(error.response.data.message, error.detailedMessage);
          setSaveInProgress(false);
        });
    }else{
      Customer.savecustomer(data)
        .then(() => {
          dialog.close({ success: true});
        })
        .catch(error => {
          notification.error(error.response.data.message, error.detailedMessage);
          setSaveInProgress(false);
        });
    }
  }
  return (
    <Modal
      {...dialog.props}
      {...(isCreateCustomertempEnabled ? {} : { footer: null })}
      title="合同单位管理"
      width={document.querySelector('body').offsetWidth*0.6}
      okText="保存"
      cancelText="取消"
      okButtonProps={{
        disabled: !isValid || saveInProgress,
        loading: saveInProgress,
        "data-test": "DashboardSaveButton",
      }}
      cancelButtonProps={{
        disabled: saveInProgress,
      }}
      onOk={handleSubmit}
      closable={!saveInProgress}
      maskClosable={!saveInProgress}
      wrapProps={{
        "data-test": "CreateDashboardDialog",
      }}>
      {dataLoaded && (
      <DynamicComponent name="CreateCustomertempDialogExtra" disabled={!isCreateCustomertempEnabled}>
      <Form layout="horizontal" {...{labelCol: { span: 4 }, wrapperCol: { span: 14 },} }>
        <Form.Item style={{ marginBottom: 5 }} label="编码" required={true}>
        <Input
          value={code}
          onChange={handleCodeChange}
          placeholder="编码"
          disabled={saveInProgress}
          autoFocus
        />
        </Form.Item>
        <Form.Item style={{ marginBottom: 5 }} label="名称" required={true}>
        <Input
          value={name}
          onChange={handleNameChange}
          placeholder="名称"
          disabled={saveInProgress}
        />
        </Form.Item>
        <Form.Item style={{ marginBottom: 5 }} label="法定代表人">
        <Input
          value={representative}
          onChange={handlerepresentativeChange}
          placeholder="法定代表人"
          disabled={saveInProgress}
        />
        </Form.Item>
        <Form.Item style={{ marginBottom: 5 }} label="电话">
        <Input
          value={mobile}
          onChange={handlemobileChange}
          placeholder="电话"
          disabled={saveInProgress}
        />
        </Form.Item>
        <Form.Item style={{ marginBottom: 5 }} label="地址">
        <Input
          value={address}
          onChange={handleaddressChange}
          placeholder="地址"
          disabled={saveInProgress}
        />
        </Form.Item>
        <Form.Item style={{ marginBottom: 5 }} label="启用">
        <Checkbox
          checked ={is_active}
          onChange={handleis_activeChange}
          disabled={saveInProgress}
          autoFocus
        />
        </Form.Item>
      </Form>
      </DynamicComponent>
      )}
    </Modal>
  );
}

CreateCustomertempDialog.propTypes = {
  Customertemp: UserProfile.isRequired,
  dialog: DialogPropType.isRequired,
};

export default wrapDialog(CreateCustomertempDialog);
